//
//  AppManager.swift
//  PMCPatient
//
//  Created by PMC on 2023/5/5.
//

import UIKit
struct YSGAppManager {
    /// 获取app版本号
    static func getLocalAppVersion() -> String {
        guard let infoDict = Bundle.main.infoDictionary,
            let versionStr = infoDict["CFBundleShortVersionString"] as? String else {
                return ""
        }
        return versionStr
    }
    
    /// 获取AppStore版本号, source: 1-Android  2-iOS  type: 1-医生端  0-患者端
    static func getAppStoreAppVersion(finished: @escaping (String, Bool, HttpCode) -> Void) {
        let paras = ["source": 2, "type": 0]
        HttpTool.shared.request(target: ApiManager.getAppVersionOfAppStore(paras: paras), success: { (json, code) in
            YSGLog("获取版本号：\(json)")
            switch code {
            case .failure:
                finished("", false, .failure)
            case .success:
                let version = json["data"]["vision"].description
                let isUpdate = json["data"]["isUpdate"].intValue
                let forceUpdate = isUpdate == 1
                
                if self.compareVersion(appstore: version, local: self.getLocalAppVersion()) {
                    finished(version, forceUpdate, .success)
                } else {
                    finished("", false, .failure)
                }
            }
        }) { (_) in
            finished("", false, .failure)
        }
    }
    
    private static func compareVersion(appstore version1: String, local version2: String) -> Bool {
        var numbers1 = version1.split(separator: ".").compactMap { Int(String($0)) }
        var numbers2 = version2.split(separator: ".").compactMap { Int(String($0)) }
        let numDiff = numbers1.count - numbers2.count
        
        if numDiff < 0 {
            numbers1.append(contentsOf: Array(repeating: 0, count: -numDiff))
        } else if numDiff > 0 {
            numbers2.append(contentsOf: Array(repeating: 0, count: numDiff))
        }
        
        for i in 0..<numbers1.count {
            let diff = numbers1[i] - numbers2[i]
            if diff != 0 {
                return diff > 0 ? true : false
            }
        }
        return false
    }
}

class YSGAppBadgeManager {
    private static let shared: YSGAppBadgeManager = YSGAppBadgeManager()
    private var unread: [String: Int] = [:]
    /// 删除某个消息下的所有角标
    public class func deleteBadge(msgId: String?) {
        guard let msgId = msgId else {
            return
        }
        
        let number = YSGAppBadgeManager.shared.unread[msgId] ?? 0
        UIApplication.shared.applicationIconBadgeNumber -= number
    }
    /// 添加某个消息下的角标
    private class func updateMsg(msgId: String?, number: Int) {
        guard let msgId = msgId else {
            return
        }
        YSGAppBadgeManager.shared.unread[msgId] = number
    }
    /// 更新所有未读消息的角标
    public class func updateBadge(array: [Advisory]) {
        var total: Int = 0
        array.forEach({
            updateMsg(msgId: $0.msgId, number: $0.unreadCount)
            total += $0.unreadCount
        })
        UIApplication.shared.applicationIconBadgeNumber = total
    }
}
